package com.xunyin.modules.mtopencity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.mtopencity.domain.MtOpenCity;
import com.xunyin.modules.mtopencity.service.MtOpenCityService;
import com.xunyin.modules.mtopencity.service.mapper.MtOpenCityMapper;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import com.xunyin.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 开通城市业务实现类
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MtOpenCityServiceImpl extends BaseServiceImpl<MtOpenCityMapper, MtOpenCity> implements MtOpenCityService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private MtOpenCityMapper mtOpenCityMapper;

    /**
     * 开通城市查询列表
     *
     * @param paginationRequest
     * @return
     */
    @Override
    public PaginationResponse<MtOpenCity> queryListByPagination(PaginationRequest paginationRequest) {
        Page<MtOpenCity> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<MtOpenCity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        String status = paginationRequest.getSearchParams().get("openStatus") == null ? "" : paginationRequest.getSearchParams().get("openStatus").toString();
        if (StringUtils.isNotBlank(status)) {
            lambdaQueryWrapper.eq(MtOpenCity::getOpenStatus, status);
        }
        lambdaQueryWrapper.eq(MtOpenCity::getIsDel, 0);
        lambdaQueryWrapper.orderByDesc(MtOpenCity::getId);
        List<MtOpenCity> dataList = mtOpenCityMapper.selectList(lambdaQueryWrapper);
        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtOpenCity> paginationResponse = new PaginationResponse(pageImpl, MtOpenCity.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(dataList);

        return paginationResponse;
    }

    /**
     * 保存开通城市信息
     *
     * @param mtOpenCity
     */
    @Override
    @Transactional
    public MtOpenCity saveMtOpenCity(MtOpenCity mtOpenCity) {
        mtOpenCity.setUpdateTime(new Date());
        if (mtOpenCity.getId() == null || mtOpenCity.getId() <= 0) {
            mtOpenCity.setCreateTime(new Date());
            this.save(mtOpenCity);
        } else {
            Integer id = mtOpenCity.getId();
            MtOpenCity mtOpenCityTemp = mtOpenCityMapper.selectById(id);
            this.updateById(mtOpenCityTemp);
        }
        return mtOpenCity;
    }

    /**
     * 根据ID获取开通城市信息
     *
     * @param id 开通城市id
     */
    @Override
    public MtOpenCity queryMtOpenCityById(Integer id) {
        return mtOpenCityMapper.selectById(id);
    }

    /**
     * 更改状态
     *
     * @param id
     */
    @Override
    public Integer updateStatus(Integer id, Integer status) {
        MtOpenCity mtOpenCity = mtOpenCityMapper.selectById(id);
        if (mtOpenCity != null) {
            mtOpenCity.setIsDel(status);
            //mtOpenCity.setOpenStatus(status);
            mtOpenCity.setUpdateTime(new Date());
            mtOpenCityMapper.updateById(mtOpenCity);

        } else {
            return 0;
        }

        return id;
    }


}
